package com.carltechnology.mahjonggamecaculation.dao.impl;

import android.content.ContentValues;

import com.carltechnology.mahjonggamecaculation.dao.IMahjongDataDao;
import com.carltechnology.mahjonggamecaculation.dao.IMahjongGameModifyComponent;
import com.carltechnology.mahjonggamecaculation.mode.IGameData;
import com.carltechnology.mahjonggamecaculation.mode.IGameRuntimeData;

import rx.Subscriber;

/**
 * Created by WZTCM on 2016/6/28.
 */
public class MahjongGameModifyComponent implements IMahjongGameModifyComponent{
    private IMahjongDataDao mDao;
    public MahjongGameModifyComponent(IMahjongDataDao dao) {
        mDao = dao;
    }
    @Override
    public void modifyMahjongDataDate(Subscriber<? super IGameData> subscriber, IGameData mode){
        mode.update();
        ContentValues values = mode.getContentValues();
        try{
            mDao.updateTable(String.valueOf(MahjongGameSqliteHelper.DATABASE_INFO.TABLE_GAMEDATA_NAME),mode,values);
            subscriber.onNext(mode);
        }catch (Exception e){
            e.printStackTrace();
            subscriber.onError(e);
        }
        subscriber.onCompleted();
    }
    @Override
    public void modifyMahjongRuntimeData(Subscriber<? super IGameRuntimeData> subscriber,IGameRuntimeData mode){
        ContentValues values = mode.getContentValues();
        try{
            mDao.updateTable(
                    String.valueOf(MahjongGameSqliteHelper.DATABASE_INFO.TABLE_RUNTIME_NAME),
                    mode,
                    values);
            subscriber.onNext(mode);
        }catch (Exception e){
            e.printStackTrace();
            subscriber.onError(e);
        }
        subscriber.onCompleted();
    }
    @Override
    public void clearRuntimeData(Subscriber<? super Boolean> subscriber){
        boolean res = false;
        try{
            res = mDao.clearRuntimeTable();
            subscriber.onNext(res);
        }catch (Exception e){
            e.printStackTrace();
            subscriber.onError(e);
        }
        subscriber.onCompleted();
    }

}
